<?php
/**
 * sh404SEF support for agora forum component.
 * @author      $Author: Peter van Westen $
 * @copyright   Peter van Westen - 2010
 * @package     sh404SEF-15
 * @license     http://www.gnu.org/copyleft/gpl.html GNU/GPL
 * @version     $Id: 3.0.14 Olympus 2010-09-28 rammstein4o $
 *				plugin version: 0002
 */

defined( '_JEXEC' ) or die( 'Direct Access to this location is not allowed.' );

// ------------------  standard plugin initialize function - don't change ---------------------------
global $sh_LANG;
$sefConfig = & shRouter::shGetConfig();
$shLangName = '';
$shLangIso = '';
$title = array();
$shItemidString = '';
$dosef = shInitializePlugin( $lang, $shLangName, $shLangIso, $option);
if ($dosef == false) return;
// ------------------  standard plugin initialize function - don't change ---------------------------

// ------------------  load language file - adjust as needed ----------------------------------------
$shLangIso = shLoadPluginLanguage( 'com_agora_lang', $shLangIso, '_SH404SEF_AGORA', JPATH_SITE.DS.'components'.DS.'com_agora'.DS.'sef_ext'.DS );
// ------------------  load language file - adjust as needed ----------------------------------------

// ------------------  parameters initialization - don't change -------------------------------------
$shAgoraParams = '';
$shAgoraParams->data = '';
$shAgoraParams->lang = $sh_LANG[$shLangIso];
// ------------------  parameters initialization - don't change -------------------------------------

// ------------------  parameters - adjust as needed ------------------------------------------------
$shAgoraParams->prefix = 'forum'; // leave blank for no prefix
$shAgoraParams->InsertCategoryPrefix = false;
$shAgoraParams->insertCategoryId = false;
$shAgoraParams->InsertCategoryName = true;
$shAgoraParams->InsertForumPrefix = false;
$shAgoraParams->InsertForumId = false;
$shAgoraParams->InsertForumName = true;
$shAgoraParams->InsertForumParents = false;
$shAgoraParams->InsertPostPrefix = false;
$shAgoraParams->InsertPostId = true;
$shAgoraParams->InsertPostSubject = true;
$shAgoraParams->InsertUserPrefix = false;
$shAgoraParams->InsertUserId = false;
$shAgoraParams->InsertUserName = true;
// ------------------  parameters - adjust as needed ------------------------------------------------

$option = isset($option) ? $option : null;
$task = isset($task) ? $task : null;
$page = isset($page) ? $page : null;
$action = isset($action) ? $action : null;
$format = isset($format) ? $format : null;

if ( in_array( $format, array( 'raw' ) ) ) {
	return;
}
switch ( $task ) {
	case 'pms':
		if ( in_array( $action, array( 'search', 'addrecipient', 'remrecipient', 'reset', 'update' ) ) ) {
			return;
		}
		break;
	case 'post':
		return;
		break;
	case 'search':
		// pass
		break;
	default:
		if ( $action ) {
			return;
		}
		break;
}


if ( !function_exists( 'shAgoraGetCategoryName' ) ) {
	function shAgoraGetCategoryName( &$params, &$title, $id, $force = 0 )
	{
		if ( !isset( $params->data->cats ) ) {
			$db =& JFactory::getDBO();
			$query  = "SELECT c.id, c.cat_name as name FROM #__agora_categories as c";
			$db->setQuery( $query );
			$params->data->cats = $db->loadObjectList( 'id' );
		}

		$name = array();

		if ( $params->InsertCategoryPrefix ) {
			$name[] = $params->lang['_SH404SEF_AGORA_CATEGORY'];
		}
		if ( isset( $params->data->cats[$id] ) ) {
			if ( $params->insertCategoryId ) {
				$name[] = $id;
			}
			if ( $force || $params->InsertCategoryName ) {
				$name[] = $params->data->cats[$id]->name;
			}
		} else {
			$name[] = $id;
		}
		$title[] = implode( '-', $name );
	}
}

if ( !function_exists( 'shAgoraGetForumName' ) ) {
	function shAgoraGetForumName( &$params, &$title, $id, $getcategory = 1, $getparents = 1, $force = 0 )
	{
		if ( !isset( $params->data->forums ) ) {
			$db =& JFactory::getDBO();
			$query  = "SELECT f.id, f.forum_name as name, parent_forum_id as parent, f.cat_id FROM #__agora_forums as f";
			$db->setQuery( $query );
			$params->data->forums = $db->loadObjectList( 'id' );
		}

		if ( isset( $params->data->forums[$id] ) ) {
			if ( $params->data->forums[$id]->cat_id && $getcategory && ( $params->insertCategoryId || $params->InsertCategoryName ) ) {
				shAgoraGetCategoryName( $params, $title, $params->data->forums[$id]->cat_id );
			}

			if ( $params->data->forums[$id]->parent && $params->InsertForumParents ) {
				shAgoraGetForumName( $params, $title, $params->data->forums[$id]->parent, 0 );
			}
		}

		$name = array();
		if ( $params->InsertForumPrefix ) {
			$name[] = $params->lang['_SH404SEF_AGORA_FORUM'];
		}
		if ( isset( $params->data->forums[$id] ) ) {
			if ( $params->InsertForumId ) {
				$name[] = $id;
			}
			if ( $force || $params->InsertForumName ) {
				$name[] = $params->data->forums[$id]->name;
			}
		} else {
			$name[] = $id;
		}
		$title[] = implode( '-', $name );
	}
}
if ( !function_exists( 'shAgoraGetTopicName' ) ) {
	function shAgoraGetTopicName( &$params, &$title, $id )
	{
		$db =& JFactory::getDBO();
		$query  = "SELECT t.id, t.subject as name, t.forum_id FROM #__agora_topics as t
			WHERE t.id = ".(int) $id."
			LIMIT 1
			";
		$db->setQuery( $query );
		$topic = $db->loadObject();

		if ( $topic && $topic->forum_id && ( $params->insertForumId || $params->InsertForumName ) ) {
			shAgoraGetForumName( $params, $title, $topic->forum_id );
		}

		$name = array();
		if ( $params->InsertPostPrefix ) {
			$name[] = $params->lang['_SH404SEF_AGORA_POST'];
		}
		if ( $topic ) {
			if ( $params->InsertPostId ) {
				$name[] = $id;
			}
			if ( $params->InsertPostSubject ) {
				$name[] = $topic->name;
			}
		} else {
			$name[] = $id;
		}
		$title[] = implode( '-', $name );
	}
}
if ( !function_exists( 'shAgoraGetTopicNameByPost' ) ) {
	function shAgoraGetTopicNameByPost( &$params, &$title, $id )
	{
		$db =& JFactory::getDBO();
		$query  = "SELECT p.topic_id FROM #__agora_posts as p
			WHERE p.id = ".(int) $id."
			LIMIT 1
			";
		$db->setQuery( $query );
		$topic = $db->loadResult();
		if ( $topic ) {
			shAgoraGetTopicName( $params, $title, $topic );
		}
	}
}
if ( !function_exists( 'shAgoraGetUserName' ) ) {
	function shAgoraGetUserName( &$params, &$title, $id )
	{
		$db =& JFactory::getDBO();
		$query  = "SELECT u.id, u.username as name FROM #__agora_users as u
			WHERE u.id = ".(int) $id."
			LIMIT 1
			";
		$db->setQuery( $query );
		$user = $db->loadObject();

		$name = array();
		if ( $params->InsertUserPrefix ) {
			$name[] = $params->lang['_SH404SEF_AGORA_USER'];
		}
		if ( $params->InsertUserId ) {
			$name[] = $user->id;
		}
		if ( $params->InsertUserName ) {
			$name[] = $user->name;
		}
		$title[] = implode( '-', $name );
	}
}

$view = isset($view) ? $view : null;
$id = isset($id) ? $id : null;
$cid = isset($cid) ? $cid : null;
$fid = isset($fid) ? $fid : null;
$source_id = isset($source_id) ? $source_id : null;
$topic_id = isset($topic_id) ? $topic_id : null;
$post_id = isset($post_id) ? $post_id : null;
$user_id = isset($user_id) ? $user_id : null;

$title = array();

if ( isset( $sefConfig->defaultComponentStringList['agora'] ) && $sefConfig->defaultComponentStringList['agora'] ) {
	$title[] = $sefConfig->defaultComponentStringList['agora'];
} else {
	$title[] = $shAgoraParams->prefix;
}

switch ( $task ) {
	case '':
	case 'category':
		// category
		//if ( $id && ( $shAgoraParams->insertCategoryId || $shAgoraParams->InsertCategoryName ) ) {
		if ( $id ) {
			//$title[] = 'category';
			shAgoraGetCategoryName( $shAgoraParams, $title, $id, ( !$shAgoraParams->insertCategoryId && !$shAgoraParams->InsertCategoryName ) );
			shRemoveFromGETVarsList('id');
			shRemoveFromGETVarsList('task');
		}  else if ( $task ) {
			$title[] = isset( $shAgoraParams->lang['_SH404SEF_AGORA_NEW'] ) ? $shAgoraParams->lang['_SH404SEF_AGORA_NEW'] : 'new';
		}
		break;
	case 'forum':
		// forum
		//if ( $id && ( $shAgoraParams->insertForumId || $shAgoraParams->InsertForumName ) ) {
		if ( $id ) {
			shAgoraGetForumName( $shAgoraParams, $title, $id, 1, 1, ( !$shAgoraParams->insertForumId && !$shAgoraParams->InsertForumName ) );
			shRemoveFromGETVarsList('id');
			shRemoveFromGETVarsList('task');
		} else {
			$title[] = isset( $shAgoraParams->lang['_SH404SEF_AGORA_NEW'] ) ? $shAgoraParams->lang['_SH404SEF_AGORA_NEW'] : 'new';
		}
		break;
	case 'topic':
		// forum
		if ( $id || $source_id ) {
			if ( ( $shAgoraParams->insertPostId || $shAgoraParams->InsertPostSubject ) ) {
				/*if ( $topic_id ) {
					shAgoraGetTopicName( $shAgoraParams, $title, $topic_id );
					$title[] = $post_id;
					shRemoveFromGETVarsList('topic_id');
					shRemoveFromGETVarsList('post_id');
				}  */
				if ( $source_id ) {
					shAgoraGetTopicName( $shAgoraParams, $title, $source_id );
					shRemoveFromGETVarsList('source_id');
					$title[] = isset( $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $action )] ) ? $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $action )] : $action;
					shRemoveFromGETVarsList('action');
				} else {
					shAgoraGetTopicName( $shAgoraParams, $title, $id );
					shRemoveFromGETVarsList('id');
				}			
			}
		} else {
			$title[] = isset( $shAgoraParams->lang['_SH404SEF_AGORA_NEW'] ) ? $shAgoraParams->lang['_SH404SEF_AGORA_NEW'] : 'new';
		}
		
		//$title[] = isset( $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $task )] ) ? $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $task )] : $task;
		shRemoveFromGETVarsList('task');
		break;
	case 'rate':
		// forum
		if ( $id ) {
			if ( ( $shAgoraParams->insertPostId || $shAgoraParams->InsertPostSubject ) ) {
				shAgoraGetTopicName( $shAgoraParams, $title, $id );
				shRemoveFromGETVarsList('id');
			}
			$title[] = isset( $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $task )] ) ? $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $task )] : $task;
		} else {
			$title[] = isset( $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $task )] ) ? $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $task )] : $task;
			$title[] = isset( $shAgoraParams->lang['_SH404SEF_AGORA_NEW'] ) ? $shAgoraParams->lang['_SH404SEF_AGORA_NEW'] : 'new';
		}
		
		shRemoveFromGETVarsList('task');
		break;
	case 'post':
		// forum
		switch ( $page ) {
			case 'topic':
			case 'poll':
				if ( $id ) {
					shAgoraGetTopicNameByPost( $shAgoraParams, $title, $id );
					shRemoveFromGETVarsList('id');
					$title[] = isset( $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $task )] ) ? $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $task )] : $task;
				} else {
					$title[] = isset( $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $task )] ) ? $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $task )] : $task;
					$title[] = isset( $shAgoraParams->lang['_SH404SEF_AGORA_NEW'] ) ? $shAgoraParams->lang['_SH404SEF_AGORA_NEW'] : 'new';
				}
				shRemoveFromGETVarsList('task');
				break;
			default:
				if ( $id ) {
					if ( $page == 'edit_topic' || ( $page == 'reply' && !$action ) ) {
						shAgoraGetTopicName( $shAgoraParams, $title, $id );
						shRemoveFromGETVarsList('id');
					} else {
						shAgoraGetTopicNameByPost( $shAgoraParams, $title, $id );
					}
					$title[] = isset( $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $task )] ) ? $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $task )] : $task;
					shRemoveFromGETVarsList('task');
					if ( $page == 'edit_topic' || ( $page == 'reply' && !$action ) ) {
						$title[] = isset( $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $page )] ) ? $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $page )] : $page;
					} else {
						$title[] = isset( $shAgoraParams->lang['_SH404SEF_AGORA_EDIT'] ) ? $shAgoraParams->lang['_SH404SEF_AGORA__EDIT'] : 'edit';
					}
					shRemoveFromGETVarsList('page');
				} else {
					$title[] = isset( $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $task )] ) ? $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $task )] : $task;
					$title[] = isset( $shAgoraParams->lang['_SH404SEF_AGORA_NEW'] ) ? $shAgoraParams->lang['_SH404SEF_AGORA_NEW'] : 'new';
				}
				break;
		}
		break;
	case 'poll':
		// poll
		if ( $page == 'select' ) {
			$title[] = isset( $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $task )] ) ? $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $task )] : $task;
			shRemoveFromGETVarsList('task');
			$title[] = isset( $shAgoraParams->lang['_SH404SEF_AGORA_NEW'] ) ? $shAgoraParams->lang['_SH404SEF_AGORA_NEW'] : 'new';
			shRemoveFromGETVarsList('page');
		}
		break;
	case 'profile':
		// forum
		$title[] = isset( $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $task )] ) ? $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $task )] : $task;
		shRemoveFromGETVarsList('task');
		if ( ( $shAgoraParams->insertUserId || $shAgoraParams->InsertUserName ) ) {
			shAgoraGetUserName( $shAgoraParams, $title, $user_id );
			shRemoveFromGETVarsList('user_id');
		}
		break;
	case 'rss':
		// feed
		if ( $cid && ( $shAgoraParams->insertCategoryId || $shAgoraParams->InsertCategoryName ) ) {
			shAgoraGetCategoryName( $shAgoraParams, $title, $cid );
			shRemoveFromGETVarsList('cid');
		} else if ( $fid && ( $shAgoraParams->insertForumId || $shAgoraParams->InsertForumName ) ) {
			shAgoraGetForumName( $shAgoraParams, $title, $fid );
			shRemoveFromGETVarsList('fid');
		}
		$title[] = $shAgoraParams->lang['_SH404SEF_AGORA_FEED'];
		shRemoveFromGETVarsList('task');
		shRemoveFromGETVarsList('format');
		shRemoveFromGETVarsList('type');
		shRemoveFromGETVarsList('key');
		break;
	default:
		$title[] = isset( $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $task )] ) ? $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $task )] : $task;
		shRemoveFromGETVarsList('task');
		break;
}

switch ( $task ) {
	case 'post':
	case 'poll':
	case 'pms':
		// do nothing
		break;
	default:
		$title[] = isset( $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $page )] ) ? $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $page )] : $page;
		$title[] = isset( $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $view )] ) ? $shAgoraParams->lang['_SH404SEF_AGORA_'.strtoupper( $view )] : $view;
		shRemoveFromGETVarsList('page');
		shRemoveFromGETVarsList('view');
		break;
}

// remove empty items from title
$title = array_diff( $title, array( '' ) );

// make certain pages not have the .html extension
switch ( $task ) {
	case '':
	case 'category':
	case 'forum':
		if ( $id ) {
			$title[] = '';
		}
		break;
}

shRemoveFromGETVarsList('option');
shRemoveFromGETVarsList('lang');
shRemoveFromGETVarsList('Itemid');
shRemoveFromGETVarsList('limit');
shRemoveFromGETVarsList('limitstart');

// ------------------  standard plugin finalize function - don't change ---------------------------
if ($dosef){
  $string = shFinalizePlugin( $string, $title, $shAppendString, $shItemidString,
  (isset($limit) ? @$limit : null), (isset($limitstart) ? @$limitstart : null),
  (isset($shLangName) ? @$shLangName : null));
}
// ------------------  standard plugin finalize function - don't change ---------------------------

